package com.haofengsoft.scce.controller;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haofengsoft.scce.db.bean.UserInfo;
import com.haofengsoft.scce.db.cache.UserCache;

/**
 * Created by weiwei on 15/6/25.
 */
public class UserController {
    private static UserController instance = null;
    private Context appContext;

    public static synchronized UserController getInstance(Context context) {
        if (instance == null) {
            instance = new UserController(context);
        }
        return instance;
    }

    public UserController(Context appContext) {
        this.appContext = appContext;
    }

    public boolean handUser(String json) {
//        			"ID": 3,
//                "UserType": null,
//                "UserAccount": "18501684692",
//                "Passward": "aslabKYkQ+k=",
//                "CreditRating": 0,
//                "UserName": null,
//                "RealName": null,
//                "Addresses": null,
//                "Email": null,
//                "Sex": null,
//                "Birthday": null,
//                "Statuses": 1,
//                "ZipCode": null,
//                "LastLoginTime": "/Date(1435216047000)/",
//                "CreateTime": "/Date(1435216047000)/",
//                "UpateTime": "/Date(1435216047000)/",
//                "StrCreateTime": "2015-06-25",
//                "StrLastLoginTime": "2015-06-25",
//                "StrUpateTime": "2015-06-25"
        try {
            JSONObject obj = JSON.parseObject(json);
            UserInfo userInfo = new UserInfo();
            userInfo.setId(obj.getIntValue("ID"));
            userInfo.setAccount(obj.getString("UserAccount"));
            userInfo.setUserType(obj.getString("UserType"));
            userInfo.setCreditRating(obj.getIntValue("CreditRating"));
            userInfo.setUserName(obj.getString("UserName"));
            userInfo.setRealName(obj.getString("RealName"));
            userInfo.setAddress(obj.getString("Addresses"));
            userInfo.setEmail(obj.getString("Email"));
            userInfo.setSex(obj.getString("sex"));
            userInfo.setBirthday(obj.getString("Birthday"));
            userInfo.setStatus(obj.getIntValue("Statuses"));
            userInfo.setZipCode(obj.getString("ZipCode"));
            userInfo.setHeadImageUrl(obj.getString("HeadImgUrl"));
            Log.d("TEXT", obj.getString("StrCreateTime"));
            userInfo.setCreateTime(obj.getString("StrCreateTime"));
            UserCache.getInstance().addUser(appContext, userInfo);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
